import { ORM } from "feok-lib";
import { emitError } from "@/model/middlewares/emitError";
import { responseFilter } from "@/model/middlewares/responseFilter";
import storage from "@/utils/Storage";
import { AxiosRequestConfig } from "axios";
const appInstall = new ORM({
  ajaxBaseUrl: process.env.VUE_APP_BASE_API_URL as string,
  ajaxTimeout: process.env.VUE_APP_AJAX_TIMEOUT as any,
  requestHook: (config: AxiosRequestConfig): AxiosRequestConfig => {
    const token = storage.session.get("token");
    if (token) {
      config.headers.token = token;
      // config.headers.token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwibmFtZSI6ImFkbWluMSIsImlhdCI6MTU4NTEwNDQwOSwiZXhwIjoxNjE2NjYyMDA5fQ.RG2dXm2vZwFPqmJESU_Rz7PQaH7UN1daqKme5Qv4h8o';
    }
    // 移除 null 和 '' 参数
    if (/[get | delete]/.test(config.method as string)) {
      Object.keys(config.params).forEach((key) => {
        if (config.params[key] === "") {
          delete config.params[key];
        }
      });
    }
    return config;
  }
});
export const app = appInstall;
export const error = emitError();
export const errorMute = emitError({ emit: false });

export const resFilter = responseFilter("data.result");
